﻿namespace WebMarkupMin.Yui
{
	using System;
	using System.Configuration;

	using Core;
	using Configuration;
	using Settings;

	/// <summary>
	/// Code minification context extensions
	/// </summary>
	public static class CodeContextExtensions
	{
		/// <summary>
		/// Configuration settings of YUI Minifier
		/// </summary>
		private static readonly Lazy<YuiConfiguration> _yuiConfig =
			new Lazy<YuiConfiguration>(() =>
				(YuiConfiguration)ConfigurationManager.GetSection("webMarkupMin/yui"));


		/// <summary>
		/// Gets a YUI CSS Minifier settings based on data from
		/// configuration files (App.config or Web.config)
		/// </summary>
		/// <param name="context">Code minification context</param>
		/// <returns>YUI CSS Minifier settings</returns>
		public static YuiCssMinificationSettings GetYuiCssMinificationSettings(this CodeContext context)
		{
			var yuiCssSettings = new YuiCssMinificationSettings();
			MapCssSettings(yuiCssSettings, _yuiConfig.Value.Css);

			return yuiCssSettings;
		}

		/// <summary>
		/// Gets a YUI JS Minifier settings based on data from
		/// configuration files (App.config or Web.config)
		/// </summary>
		/// <param name="context">Code minification context</param>
		/// <returns>YUI JS Minifier settings</returns>
		public static YuiJsMinificationSettings GetYuiJsMinificationSettings(this CodeContext context)
		{
			var yuiJsSettings = new YuiJsMinificationSettings();
			MapJsSettings(yuiJsSettings, _yuiConfig.Value.Js);

			return yuiJsSettings;
		}

		/// <summary>
		/// Maps a common settings
		/// </summary>
		/// <param name="commonSettings">Common settings of YUI Minifier</param>
		/// <param name="commonConfig">Common configuration settings of YUI Minifier</param>
		private static void MapCommonSettings(YuiCommonMinificationSettingsBase commonSettings,
			YuiCommonMinificationConfigurationBase commonConfig)
		{
			commonSettings.LineBreakPosition = commonConfig.LineBreakPosition;
		}

		/// <summary>
		/// Maps a CSS settings
		/// </summary>
		/// <param name="cssSettings">YUI CSS Minifier settings</param>
		/// <param name="cssConfig">Configuration settings of YUI CSS Minifier</param>
		private static void MapCssSettings(YuiCssMinificationSettings cssSettings,
			YuiCssMinificationConfiguration cssConfig)
		{
			MapCommonSettings(cssSettings, cssConfig);

			cssSettings.RemoveComments = cssConfig.RemoveComments;
		}

		/// <summary>
		/// Maps a JS settings
		/// </summary>
		/// <param name="jsSettings">YUI JS Minifier settings</param>
		/// <param name="jsConfig">Configuration settings of YUI JS Minifier</param>
		private static void MapJsSettings(YuiJsMinificationSettings jsSettings,
			YuiJsMinificationConfiguration jsConfig)
		{
			MapCommonSettings(jsSettings, jsConfig);

			jsSettings.ObfuscateJavascript = jsConfig.ObfuscateJavascript;
			jsSettings.PreserveAllSemicolons = jsConfig.PreserveAllSemicolons;
			jsSettings.DisableOptimizations = jsConfig.DisableOptimizations;
			jsSettings.IgnoreEval = jsConfig.IgnoreEval;
		}
	}
}